package com.qqs.distributed.lock;

import com.qqs.distributed.advice.CacheAttributeWrapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

public class RedissonLockHandler extends AbstractLockHandler {
    private RLock lock;
    private RedissonClient client;

    public RedissonLockHandler(
            CacheAttributeWrapper attributeWrapper,
            RedissonClient client) {
        this(attributeWrapper);
        this.client = client;
    }

    protected RedissonLockHandler(CacheAttributeWrapper attributeWrapper) {
        super(attributeWrapper);
    }

    @Override
    public void lock() {
        lock = client.getLock("look:" + attributeWrapper.getKey().intern());
        lock.lock(attribute.getLockTtl(), attribute.getLockTimeUnit());
    }

    @Override
    public void unlock() {
        if (lock != null) {
            lock.unlock();
        }
    }
}
